﻿using System;
using System.Runtime.Serialization;
using System.Windows;
using System.Windows.Input;
using Foundation;
using Poet.Properties;
using Poet.Views;

namespace Poet.ViewModels
{
    [DataContract]
    public class RecentFileViewModel : ViewModel
    {
        public RecentFileViewModel()
        {
            SerializeFileName = System.IO.Path.Combine(Settings.Default.RecentFilesDirectory, Guid.NewGuid() + ".json");
            SerializeFileName = PathProvider.FindFileByName(SerializeFileName);
            Initialize();
        }

        [DataMember]
        public string SerializeFileName { get; private set; }

        [DataMember]
        public string Path
        {
            get { return Get(() => Path); }
            set { Set(() => Path, value); }
        }

        [DataMember]
        public bool IsPinned
        {
            get { return Get(() => IsPinned); }
            set { Set(() => IsPinned, value); }    
        }

        public bool IsOpened
        {
            get { return RestoredView != null; }
            set
            {
                if (value && RestoredView == null)
                    ApplicationCommands.Open.Execute(new[] {Path}, Application.Current.MainWindow);
                if (!value && RestoredView != null) 
                    ApplicationCommands.Close.Execute(null, RestoredView);
            }
        }

        private DocumentView RestoredView
        {
            get { return Get(() => RestoredView); }
            set
            {
                Set(() => RestoredView, value);
                RaisePropertyChangedEvent(() => IsOpened);
            }
        }

        internal DocumentView RestoreView()
        {
            //SerializeFileName = SerializeFileName.GetFileNameForSerialization();
            var viewModel = Serializer.TryDeserializeDataContract<DocumentViewModel>(SerializeFileName) ??
                   new DocumentViewModel(Path);
            RestoredView = new DocumentView {DataContext = viewModel};
            RestoredView.Closed += (sender, args) => RestoredView = null;
            return RestoredView;
        }

        private void StoreView(DocumentView view)
        {
            //SerializeFileName = SerializeFileName.GetFileNameForSerialization();
            var viewModel = (DocumentViewModel) view.DataContext;
            viewModel.SerializeDataContractAsync(SerializeFileName);
        }

        [OnDeserialized]
        private void Initialize(StreamingContext context = default(StreamingContext))
        {
            this[() => RestoredView].PropertyChanged += (o, e) =>
                {
                    if (RestoredView != null)
                        RestoredView.Closed += (sender, args) =>
                        {
                            StoreView(RestoredView);
                            RestoredView.Dispose();
                            RestoredView = null;
                        };
                };
        }
    }
}
